package com.example.music.ui.fragments

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.music.R
import com.example.music.ui.adapters.MusicAdapter

class SearchFragment : BaseFragment() {
    
    private lateinit var musicAdapter: MusicAdapter
    
    override fun getLayoutRes(): Int = R.layout.fragment_search
    
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return super.onCreateView(inflater, container, savedInstanceState)
    }
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setupRecyclerView()
        observeViewModel()
        setupSearchListener()
    }
    
    private fun setupRecyclerView() {
        musicAdapter = MusicAdapter { music ->
            viewModel.playMusic(music)
        }
        
        val searchRecyclerView = view?.findViewById<androidx.recyclerview.widget.RecyclerView>(R.id.searchRecyclerView)
        searchRecyclerView?.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = musicAdapter
        }
    }
    
    private fun observeViewModel() {
        viewModel.musicList.observe(viewLifecycleOwner, Observer { musicList ->
            val searchRecyclerView = view?.findViewById<androidx.recyclerview.widget.RecyclerView>(R.id.searchRecyclerView)
            val searchEmptyState = view?.findViewById<android.widget.LinearLayout>(R.id.searchEmptyState)
            
            if (musicList.isEmpty()) {
                searchRecyclerView?.visibility = View.GONE
                searchEmptyState?.visibility = View.VISIBLE
            } else {
                searchRecyclerView?.visibility = View.VISIBLE
                searchEmptyState?.visibility = View.GONE
                musicAdapter.updateMusicList(musicList)
            }
        })
        
        viewModel.currentMusic.observe(viewLifecycleOwner, Observer { currentMusic ->
            musicAdapter.setCurrentMusic(currentMusic)
        })
    }
    
    private fun setupSearchListener() {
        val searchEditText = view?.findViewById<android.widget.EditText>(R.id.searchEditText)
        searchEditText?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                val query = s.toString()
                if (query.isNotEmpty()) {
                    viewModel.searchMusic(query)
                } else {
                    // 重新加载所有音乐
                    viewModel.searchMusic("")
                }
            }
            
            override fun afterTextChanged(s: Editable?) {}
        })
    }
}
